package com.central.apps.handler.supplier;

import com.alibaba.fastjson.JSONObject;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//供应商准入
@Component
@Slf4j
public class SupplierAccessHandler extends BaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("suppaccApp".equals(app)){
            if ("getsuppacc".equals(type)){
                JSONObject object = new JSONObject();

                String id = MapUtils.getString(params, "id");
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select application from gq_suppacc where id='" + id + "'");
                if (CollectionUtils.isNotEmpty(bySql1)){
                    Map<String, Object> map1 = bySql1.get(0);
                    String application = MapUtils.getString(map1, "application");
                    //解决未选择供应商情况下，不让计算得分，应该提示请先创建供应商或者计算无效
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("select count(0) as count from gq_account where application = '" + application + "'");
                    if(CollectionUtils.isNotEmpty(bySql2)){
                        if(MapUtils.getInteger(bySql2.get(0),"count")==0){
                            object.put("code","1");
                            object.put("msg", "请先创建供应商或者计算无效!");
                            return object.toString() ;
                        }
                    }
                    List<Map<String, Object>> bySql = handlerService.findBySql("\tSELECT score from gq_evalsuppliers where application ='" + application + "'");
                    float num =0;
                    if (CollectionUtils.isNotEmpty(bySql)){
                        for (int i=0;i<bySql.size();i++){
                            Map<String, Object> map = bySql.get(i);
                            Float score = MapUtils.getFloat(map, "score");
                            num = num + score;
                        }
                        if ( num <=100 && num >=0){
                            object.put("code","0");
                            handlerService.updateBySql("\tUPDATE gq_account set assessment ="+num+" where application ='"+application+"'");
                            object.put("msg", "计算得分成功!");
                            return object.toString() ;
                        }else{
                            object.put("code","1");
                            object.put("msg", "评定分数不能小于0并且不能超过100!");
                            return object.toString() ;

                        }

                }
                }
            }
        }
        return super.operate(app, type, params);
    }

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if ("submit".equals(flowNode.getId())){
            List<Map<String, Object>> bySql = handlerService.findBySql("select application from gq_suppacc where id =" + id);
            Map<String, Object> map = bySql.get(0);
            String application = MapUtils.getString(map, "application");

            List<Map<String, Object>> bySql1 = handlerService.findBySql("select supplierno,assessment from gq_account where application ='" + application + "'");
            List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_evalsuppliers where application='" + application + "'");
            if (CollectionUtils.isEmpty(bySql1)){
                return "请添加供应商列表相关信息！";
            }else if (CollectionUtils.isNotEmpty(bySql1)){
                for (int i=0;i<bySql1.size();i++){
                    Map<String, Object> map1 = bySql1.get(i);
                    Float assessment = MapUtils.getFloat(map1, "assessment");
                    if (assessment>=60&&assessment<70){
                        return "请根据评定的相关问题进行整改";
                    }else if(assessment==0){
                        return "评定分数为0，请计算分数";
                    }else if (assessment<60){
                        return "评定分数小于60，无法提交审批";
                    }
                }

            }else if (CollectionUtils.isNotEmpty(bySql2)){
                return "请添加评定标准！";
            }
        }
        return super.beforeExcuteFlow(app, id, flowNode, op);
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if (flowNode.getId().equals("approvaler")){
            boolean flag = op.isFlag();
            if (flag){
                List<String> accountidList =new ArrayList<>();
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_suppacc where id=" + id);
                String application = BLuntil.getParameter(bySql.get(0), "application");
                List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT id,createtime,assessment,type from gq_account where application ='"+application+"'");
                if (CollectionUtils.isNotEmpty(bySql1)){
                    for (int i=0;i<bySql1.size();i++){
                        Map<String, Object> map = bySql1.get(i);
                        String createtime = MapUtils.getString(map, "createtime");
                        String type = MapUtils.getString(map, "type");
                        Float assessment = MapUtils.getFloat(map, "assessment");
                        String accountId = MapUtils.getString(map, "id");//供应商台账id
                        if (assessment>=70){
                            String supliergrade ="试用";
                            type="1";
                           handlerService.updateBySql("UPDATE gq_account set createtime = CURDATE(),supliergrade ='"+supliergrade+"',type='"+type+"' where application = '"+application+"'");
                            accountidList.add(accountId);
                        }
                    }
                }
                List<Map<String, Object>> bySql2 = handlerService.findBySql("select fileid from tb_file where appname in('zz_1666002876000_66811','pd_1666002878000_39536') and recordid ="+id);
                for(Map<String, Object> map:bySql2){
                    String fileid =MapUtils.getString(map,"fileid");
                    for(String accountid:accountidList){
                        handlerService.updateBySql("INSERT INTO tb_file (\n" +
                                "\t`pathid`,\n" +
                                "\t`deptid`,\n" +
                                "\t`deptfullname`,\n" +
                                "\t`userid`,\n" +
                                "\t`username`,\n" +
                                "\t`uploadtime`,\n" +
                                "\t`filesize`,\n" +
                                "\t`filepath`,\n" +
                                "\t`extensionname`,\n" +
                                "\t`appid`,\n" +
                                "\t`appname`,\n" +
                                "\t`recordid`,\n" +
                                "\t`validdate`,\n" +
                                "\t`invaliddate`,\n" +
                                "\t`filestatus`,\n" +
                                "\t`statustime`,\n" +
                                "\t`filename`,\n" +
                                "\t`filerealname`,\n" +
                                "\t`fileversion`,\n" +
                                "\t`md5`,\n" +
                                "\t`refertab`,\n" +
                                "\t`referid`,\n" +
                                "\t`secretkey`,\n" +
                                "\t`isshow`,\n" +
                                "\t`status`\n" +
                                ")\n" +
                                "select \n" +
                                "\tpathid,\n" +
                                "\tdeptid,\n" +
                                "\tdeptfullname,\n" +
                                "\tuserid,\n" +
                                "\tusername,\n" +
                                "\tuploadtime,\n" +
                                "\tfilesize,\n" +
                                "\tfilepath,\n" +
                                "\textensionname,\n" +
                                "\tappid,\n" +
                                "\t'accountApp',\n" +
                                "\t'"+accountid+"',\n" +
                                "\tvaliddate,\n" +
                                "\tinvaliddate,\n" +
                                "\tfilestatus,\n" +
                                "\tstatustime,\n" +
                                "\tfilename,\n" +
                                "\tfilerealname,\n" +
                                "\tfileversion,\n" +
                                "\tmd5,\n" +
                                "\trefertab,\n" +
                                "\treferid,\n" +
                                "\tsecretkey,\n" +
                                "\tisshow,\n" +
                                "\tstatus\n" +
                                "from \n" +
                                "tb_file where fileid ="+fileid);
                    }
                }
            }
        }
        super.afterExcuteFlow(app, id, flowNode, op, event);
    }
}
